<?php
use yii\helpers\Html;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel common\models\log\ConfigFunctionlogSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Config Functionlogs');
$pageSize = \Yii::$app->params['pageSize'];
$limitsJson = \Yii::$app->params['limitsJson'];

?>
<!-- <div id="demo1"></div> -->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" readonly placeholder="查询加入开始时间" name="start" id="start">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" readonly placeholder="查询加入结束时间" name="end" id="end">
                        </div>
                        <!-- <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="" id="name" placeholder="用户名" autocomplete="off" class="layui-input">
                        </div> -->
                        <div class="layui-inline layui-show-xs-block" style="width: 168px;">                        
                            <div id="username"> </div>                           
                            <!-- <input class="layui-input" autocomplete="off" id="123"> -->
                            
                            <!-- <select name="username" lay-verify="" lay-search >
                                <option value="010">layer</option>
                                <option value="021">form</option>
                                <option value="0571">layim</option>
                            </select>  -->
                            
                        </div>

                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" type="button" id="sreach" data-type="sreach" lay-filter="sreach">
                                <i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>

                <div class="layui-card-body ">
                    <table class="layui-table" id="constantList" lay-filter="constantList">

                    </table>
                </div>

            </div>
        </div>
    </div>
</div>

<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-danger">批量删除</button>
        <button class="layui-btn"><i class="layui-icon"></i>添加</button>

    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="view">查看</a>
   <!--  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> -->
</script>

<script>
    $(function() {
        layui.use('laydate',
            function() {
                var laydate = layui.laydate;
                //执行一个laydate实例
                laydate.render({
                    elem: '#start' //指定元素
                });
                //执行一个laydate实例
                laydate.render({
                    elem: '#end' //指定元素
                });
            });
    })

    var xmUsername = xmSelect.render({
		el: '#username',
		language: 'zn',
        size: 'mini',
        tips: '请选择访问人',
        searchTips: '输入姓名进行查询',
        style:{minHeight:'30px',},        
        model: {
            label: {
                type: 'block',
                block: {
                    //最大显示数量, 0:不限制
                    showCount: 1,
                    //是否显示删除图标
                    showIcon: false,
                }
            },
        },
        
        autoRow: true,
		toolbar: { show: true },
		filterable: true,
		remoteSearch: true,
        prop: {
            name: 'username',
            value: 'user_id',
	    },
        
		remoteMethod: function(val, cb, show){
            var _this = xmUsername;
			//这里如果val为空, 则不触发搜索
			if(!val){
                var data = [];                
                if(xmUsername!=undefined){
                    data = xmUsername.getValue();
                }                
                return cb(data);
			}

            Cajax({
                    type:"POST",
                    url:"<?php echo \yii\helpers\Url::toRoute(['/auth/get-admin-list']);?>",
                    data:{
                        _csrfBackend:'<?=\Yii::$app->request->csrfToken?>',
                        username_search: val
                    }
                },function(){
                    cb([]);
                },function(JsonData){
                    console.log(xmUsername);
                    var res = JsonData;
                    data =res.data;
				    cb(data);

                   
                }) 
		},
	})


</script>

<?php

$listUrl = \yii\helpers\Url::toRoute(['list']);
$viewUrl = \yii\helpers\Url::toRoute(['view']);
$updateUrl = \yii\helpers\Url::toRoute(['update']);
$delUrl = \yii\helpers\Url::toRoute(['delete']);
$addUrl = \yii\helpers\Url::toRoute(['create']);
$_csrfBackend = \Yii::$app->request->csrfToken;
$this->registerJsFile('@web/js/xm-select.js',['position'=>\yii\web\View::POS_BEGIN]);

$tableJs = <<<JS

    


    function rowDel(obj,data,index) {
            if(data.length == 0){
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
            }
            var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
            layer.confirm('真的删除吗？', function(index){
                //var url = "$delUrl";
                //var urlArgs = {id:id} ; 
                //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                console.log(id);

                Cajax({
                    type:"POST",
                    url:"$delUrl",
                    data:{_csrfBackend:'$_csrfBackend',id:id}
                },function(){},function(JsonData){
                    layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                    if(JsonData.status){
                        //obj.del();
                        obj.reload('constantList', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        }, 'data');
                        layer.close(index);
                    }
                })
            });
       }
    function delSelected(obj,data,index){
        if(data.length == 0){
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
            }
        var id = (data.length > 0) ? getColumn(data,'id') : data.id;

        layer.confirm('真的删除吗？', function(index){
            //obj.del();
            console.log(obj);
            console.log(data);
            console.log(index);
            //layer.close(index);

            var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
            layer.confirm('真的删除吗？', function(index){
                //var url = "$delUrl";
                //var urlArgs = {id:id} ; 
                //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                

                Cajax({
                    type:"POST",
                    url:"$delUrl",
                    data:{_csrfBackend:'$_csrfBackend',id:id}
                },function(){},function(JsonData){
                    layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                    if(JsonData.status){
                        obj.del();
                        layer.close(index);
                    }
                })
            });


        })
    }
    layui.use(['table','util'],function() {
        var table = layui.table,
            form = layui.form;
        //var util = layui.util;
        var renderOpt = {
            id:'constantList',
            elem:'#constantList',
            toolbar:"#toolbar",
            url: '$listUrl', //数据接口
            method:"POST", //
            where:{_csrfBackend:'$_csrfBackend'},
            page: true, //开启分页
            limit: $pageSize,
            limits: $limitsJson,
            request:{
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            cols: [[
                {field: 'id', checkbox:true,title: 'ID', width:80, sort: true, fixed: 'left'},
                {field: 'id', title: '主键',minWidth: 80},
                {field: 'app_id', title: '应用id',minWidth: 80},
                {field: 'merchant_id', title: '商户id',minWidth: 80},
                //{field: 'user_id', title: '用户id',minWidth: 80},
                {field: 'visiter', title: '用户名',minWidth: 80},
                {field: 'method', title: '提交类型',minWidth: 80},
                {field: 'module', title: '模块',minWidth: 80},
                {field: 'controller', title: '控制器',minWidth: 80},
                {field: 'action', title: '方法',minWidth: 80},
                {field: 'url', title: '提交url',minWidth: 80},
                {field: 'get_data', title: 'get数据',templet:'<div>{{d.get_data}}</div>',minWidth: 80},
                {field: 'post_data', title: 'post数据',templet:function(d){ 
                        let str = d.post_data;
                        return '<div>'+ str +'</div>';
                    },minWidth: 80},
                {field: 'header_data', title: 'header数据',minWidth: 80},
                {field: 'ip', title: 'ip地址',minWidth: 80},
                {field: 'error_code', title: '报错code',minWidth: 80},
                {field: 'error_msg', title: '报错信息',minWidth: 80},
                {field: 'error_data', title: '报错日志',minWidth: 80},
                {field: 'user_agent', title: 'UA信息',minWidth: 80},
                {field: 'device', title: '设备信息',minWidth: 80},
                {field: 'device_uuid', title: '设备唯一号',minWidth: 80},
                {field: 'device_version', title: '设备版本',minWidth: 80},
                {field: 'device_app_version', title: '设备app版本',minWidth: 80},
                {field: 'siteid', title: '站点id',minWidth: 80},
                {field: 'is_del', title: '是否删除',minWidth: 80},
                
                /* {field: 'created_at', title: '添加时间',templet: function(d){ 
                        var star_addtime = util.toDateString(d.created_at*1000, "yyyy-MM-dd HH:mm:ss");
                        //var star_addtime = d.created_at;
                        return '<div>'+ star_addtime +'</div>';
                    },minWidth: 80}, */

                //日期转换需要引用util组件,layui.use(['table','util'] ,然后toDateString的日期参数是毫秒级的,unix时间戳是秒级的,所以时间戳需要*1000,也可以使用函数或者模板的方式转换
                {field: 'created_at', title: '添加时间',templet:'<div>{{layui.util.toDateString(d.created_at*1000)}}</div>',minWidth: 80}, 
                {field: 'updated_at', title: '修改时间',templet:'<div>{{layui.util.toDateString(d.updated_at*1000)}}</div>',minWidth: 80},
                {field: 'creater', title: '添加人',minWidth: 80},
                {field: 'updater', title: '修改人',minWidth: 80},
                {fixed: 'right', title:'操作', toolbar: '#barDemo', width:80},
            ]]
        };
        table.render(renderOpt);
        //头工具栏事件
        table.on('toolbar(constantList)',
            function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：' + data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;
                    case 'delAll':
                        var data = checkStatus.data;
                        /* rowDel(obj,data); */

                        if(data.length == 0){
                            layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                            return false;
                        }
                        var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
                        layer.confirm('真的删除吗？', function(index){
                            //var url = "$delUrl";
                            //var urlArgs = {id:id} ; 
                            //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                            console.log(obj);
                            /* console.log(id);
                            console.log(obj);
                            console.log(data);
                            console.log(index);
                            console.log(table); */
                            //console.log(obj.config.page.curr);
                            
                            Cajax({
                                type:"POST",
                                url:"$delUrl",
                                data:{_csrfBackend:'$_csrfBackend',id:id}
                            },function(){},function(JsonData){
                                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                                if(JsonData.status){
                                    //obj.del();                                    
                                    
                                    table.reload('constantList', {  
                                        where:{
                                        _csrfBackend:'$_csrfBackend',
                                        
                                        }  ,                               
                                        page: {
                                            curr: obj.config.page.curr, //当前页重新加载
                                            pageSize:obj.config.limit,
                                        }
                                    }, 'data');
                                    layer.close(index);
                                }
                            })
                        });




                        break;
                    
                    case 'refresh':
                        //执行重载
                        table.reload('constantList', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        }, 'data');
                        break;
                };
            });
        //监听行工具事件
        table.on('tool(constantList)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                //rowDel(obj,data);
                if(data.length == 0){
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
                }
                var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
                layer.confirm('真的删除吗？', function(index){
                    //var url = "$delUrl";
                    //var urlArgs = {id:id} ; 
                    //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                    console.log(id);

                    Cajax({
                        type:"POST",
                        url:"$delUrl",
                        data:{_csrfBackend:'$_csrfBackend',id:[id]}
                    },function(){},function(JsonData){
                        layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                        if(JsonData.status){

                            console.log(table);
                            table.reload('constantList', {  
                                        where:{
                                        _csrfBackend:'$_csrfBackend',
                                        
                                        }  ,                               
                                        page: {
                                            //page: table.config.page.curr, //当前页重新加载
                                            page: $(".layui-laypage-skip .layui-input").val(),
                                            pageSize: $(".layui-laypage-limits").find("option:selected").val(),
                                        }
                            }, 'data');
                            layer.close(index);

                        }
                    })
                });    





            } else if(obj.event === 'edit'){
                var url = "$editUrl";
                var urlArgs = {id:data.id} ; 
                layer.open({
                    type: 2,
                    offset: 't',
                    content: attachUrlArgs(url,urlArgs),
                    area:['60%','80%'],
                    title:'编辑'
                });


                //xadmin.open('修改用户','$addAdmin?userId='+data.user_id,500,550)
            } else if(obj.event === 'view'){
                var url = '$viewUrl';
                var urlArgs = {id:data.id} ; 
                //xadmin.open('查看11',attachUrlArgs(url,urlArgs),500,550);
                layer.open({
                    type: 2,
                    offset: 't',
                    content: attachUrlArgs(url,urlArgs), // url?id='+data.id
                    area:['60%','80%'],
                    title:'查看'
                });
            } else if(obj.event === 'member_stop'){
                member_stop(obj);
            }
        });
        form.on('checkbox(lockDemo)', function(obj){
            var _this = obj.othis;
            var checked = (obj.elem.checked == true) ? 1 : 0;
            Cajax({
                type:"POST",
                url:'$updateUrl',
                data:{_csrfBackend:_csrfBackend,id:this.value,is_hide_des:checked}
            },function () {},function(JsonData){
                if(JsonData.status == false){//layui-form-checked
                    (checked == 1) ? $(_this).parent('div').removeClass('layui-form-checked') : $(_this).parent('div').addClass('layui-form-checked');
                }
                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
            })
        });

        form.on('checkbox(lockStatus)', function(obj){
            var _this = obj.othis;
            var checked = (obj.elem.checked == true) ? 1 : 0;
            Cajax({
                type:"POST",
                url:'$updateUrl',
                data:{_csrfBackend:'$_csrfBackend',id:this.value,status:checked}
            },function () {},function(JsonData){
                if(JsonData.status == false){//layui-form-checked
                    (checked == 1) ? $(_this).parent('div').removeClass('layui-form-checked') : $(_this).parent('div').addClass('layui-form-checked');
                }
                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
            })
        });
        //搜索
         var $ = layui.$, active = {
            sreach: function(){
                var start = $('#start');
                var end = $('#end');
                var usernames = xmUsername.getValue('valueStr');
                //执行重载
                table.reload('constantList', {
                    //url:'$getBaseConfigList',
                    page: { curr: 1 } ,
                    where: {
                        start: start.val(),
                        end: end.val(),
                        usernames: usernames
                    }
                }, 'data');
            }
          };
          $('#sreach').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
          });
     }); 
JS;
$this->registerJs($tableJs, \yii\web\View::POS_END);
?>
